package com.xmy.aboutIO;

import java.io.*;

// InputStream
public class Demo2 {

    public static void main(String[] args) throws IOException {

        String name = "E:\\IdeaProjects\\README.md";

        // File file = new File(name);

        // readFileByBuffer(file);

        // printByteArray();

        File file = new File("E:\\IdeaProjects\\Hello\\IO\\src\\com\\xmy\\aboutIO\\test.txt");
        /*try(InputStream stream = new FileInputStream(file)) {

            String s = readFileString(stream);

            System.out.println(s);
        }*/
        FileReader reader = new FileReader(file);

        //readFile(reader);

        System.out.println("============================");

        readFileString(file, "utf-8");
    }

    // 逐个字节读取
    public static void readFileByByte(File file) throws IOException {

        InputStream inputStream = new FileInputStream(file);

        int n;

        while ((n = inputStream.read()) != -1) {
            System.out.println(n);
        }

        if (inputStream != null) {

            inputStream.close();
        }
    }

    // 缓冲读取,一次性读取多个字节到缓冲区 FileInputStream
    public static void readFileByBuffer(File file) throws IOException {
        try(InputStream stream = new FileInputStream(file)) {

            byte[] buffer = new byte[1024];

            int n;

            while ((n = stream.read(buffer)) != -1) {
                System.out.println(n + " bytes are read");
            }
        }
    }

    //InputStream 的实现类 ByteArrayInputStream 在内存中模拟输入流InputStream
    public static void printByteArray() throws IOException {

        byte[] data = { 72, 101, 108, 108, 111, 33 };

        try(InputStream stream = new ByteArrayInputStream(data)) {

            int n;

            while ((n = stream.read()) != -1) {

                System.out.print((char) n);
            }
        }
    }

    //
    public static String readFileString(InputStream stream) throws IOException {

        StringBuilder s = new StringBuilder();

        int n;

        while ((n = stream.read()) != -1) {
            s.append((char) n);
        }

        return s.toString();
    }

    // // 流转换：将字节流转为字符流，按指定格式字符格式
    public static void readFileString(File file, String charSet) {

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charSet));

            String line;

            while ((line = reader.readLine()) != null) {

                System.out.println(line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 流转换：将字节流转为字符流，按默认字符格式
    public static void readFileString(File file) {

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String line;

            while ((line = reader.readLine()) != null) {

                System.out.println(line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 以字符读取
    public static void readFile(FileReader fileReader) {
        try {

            BufferedReader reader = new BufferedReader(fileReader);

            String str;

            while ((str = reader.readLine()) != null) {

                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
